<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';

interface NavItem {
  label: string;
  path: string;
}

interface Props {
  title?: string;
  logo?: string;
  navItems?: NavItem[];
  userName?: string;
  userAvatar?: string;
}

const props = withDefaults(defineProps<Props>(), {
  title: 'Vue App',
  logo: '',
  navItems: () => [
    { label: '首页', path: '/' },
    { label: '关于', path: '/about' },
    { label: '服务', path: '/services' },
    { label: '联系', path: '/contact' }
  ],
  userName: '用户',
  userAvatar: ''
});

const emit = defineEmits<{
  (e: 'navClick', item: NavItem): void;
  (e: 'userMenuClick', action: string): void;
}>();

const showMenu = ref(false);

const handleNavClick = (item: NavItem) => {
  emit('navClick', item);
};

const handleUserMenuClick = (action: string) => {
  emit('userMenuClick', action);
  showMenu.value = false;
};

// 点击外部关闭下拉菜单
const handleClickOutside = (e: MouseEvent) => {
  const target = e.target as HTMLElement;
  if (!target.closest('.user-menu')) {
    showMenu.value = false;
  }
};

onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});
</script>

<template>
  <header class="header-nav">
    <!-- 左侧 Logo + 标题 -->
    <div class="header-left">
      <div v-if="logo" class="logo">
        <img :src="logo" :alt="title" />
      </div>
      <h1 class="title">{{ title }}</h1>
    </div>

    <!-- 中间导航链接 -->
    <nav class="nav-links">
      <router-link
        v-for="(item, index) in navItems"
        :key="index"
        :to="item.path"
        class="nav-link"
        @click="handleNavClick(item)"
      >
        {{ item.label }}
      </router-link>
    </nav>

    <!-- 右侧用户菜单 -->
    <div class="header-right">
      <div class="user-menu">
        <div v-if="userAvatar" class="user-avatar">
          <img :src="userAvatar" :alt="userName" />
        </div>
        <span class="user-name">{{ userName }}</span>
        <div class="dropdown-menu">
          <button class="dropdown-toggle" @click="showMenu = !showMenu">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </button>
          <div v-if="showMenu" class="dropdown-content">
            <button class="dropdown-item" @click="handleUserMenuClick('profile')">个人资料</button>
            <button class="dropdown-item" @click="handleUserMenuClick('settings')">设置</button>
            <button class="dropdown-item" @click="handleUserMenuClick('logout')">退出登录</button>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>



<style scoped>
.header-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0 24px;
  height: 64px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo img {
  height: 40px;
  width: auto;
  object-fit: contain;
}

.title {
  font-size: 20px;
  font-weight: 600;
  color: #333333;
  margin: 0;
}

.nav-links {
  display: flex;
  gap: 32px;
}

.nav-link {
  text-decoration: none;
  color: #666666;
  font-size: 16px;
  font-weight: 500;
  padding: 8px 0;
  position: relative;
  transition: color 0.3s ease;
}

.nav-link:hover {
  color: #1890ff;
}

.nav-link.router-link-active {
  color: #1890ff;
}

.nav-link.router-link-active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #1890ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
  cursor: pointer;
}

.user-avatar img {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
}

.user-name {
  font-size: 14px;
  font-weight: 500;
  color: #333333;
}

.dropdown-toggle {
  background: none;
  border: none;
  color: #666666;
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dropdown-toggle:hover {
  color: #1890ff;
}

.dropdown-content {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  padding: 8px 0;
  min-width: 160px;
  margin-top: 8px;
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 8px 16px;
  background: none;
  border: none;
  text-align: left;
  font-size: 14px;
  color: #333333;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.dropdown-item:hover {
  background-color: #f0f0f0;
  color: #1890ff;
}
</style>